package com.example.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class PriorityQueueConfig {

    private static final String PRIORITY_EXCHANGE_NAME = "priority.exchange";
    private static final String PRIORITY_QUEUE_NAME = "priority.queue";

    @Bean
    public DirectExchange priorityExchange() {
        return ExchangeBuilder.directExchange(PRIORITY_EXCHANGE_NAME).build();
    }

    @Bean
    public Queue priorityQueue() {
        Map<String, Object> arguments = new HashMap<>();
        //设置队列优先级为0-10
        arguments.put("x-max-priority", 10);
        return QueueBuilder
                .durable(PRIORITY_QUEUE_NAME)
                .withArguments(arguments)
                .build();
    }

    @Bean
    public Binding priorityQueueBindingPriorityExchange() {
        return BindingBuilder.bind(priorityQueue()).to(priorityExchange()).with("");
    }
}
